<?php

/**
 * 此示例为 微信支付（扫码支付 二）
 * https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1
 */

require './config.php';

use Endroid\QrCode\QrCode;
use Thenbsp\Wechat\Util\Util;
use Thenbsp\Wechat\Util\SignGenerator;

use Thenbsp\Wechat\Payment\Order;
use Thenbsp\Wechat\Payment\Business;
use Thenbsp\Wechat\Payment\Unifiedorder;

/**
 * 第 1 步：定义商户
 */
$businessA = new Business();
$businessA->appid(APPID);
$businessA->appsecret(APPSECRET);
$businessA->mch_id(MCHID);
$businessA->mch_key(MCHKEY);

// 可以配置多个商户
// $businessB = new Business();
// $businessB->appid(APPID);
// $businessB->appsecret(APPSECRET);
// $businessB->mch_id(MCHID);
// $businessB->mch_key(MCHKEY);

/**
 * 第 2 步：定义订单（可链式调用）
 */
$order = new Order();
$order->body('iphone 6 plus');
$order->out_trade_no(date('Y-m-dHis').mt_rand(10000, 99999));
$order->total_fee('1');
$order->trade_type('NATIVE');
$order->notify_url('Your notify url');

/**
 * 第 3 步：统一下单
 */
$unifiedorder   = new Unifiedorder($businessA, $order);
$response       = $unifiedorder->getResponse();

if( !isset($response['code_url']) ) {
    exit($e->getMessage());
}

/**
 * 生成支付二维码
 * See https://github.com/endroid/QrCode
 */
$qrCode = new QrCode();
$qrCode
    ->setText($response['code_url'])
    ->setSize(200)
    ->setPadding(10)
    ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
    ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
;

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Wechat SDK</title>
</head>
<body ontouchstart="">

<h1>扫描支付模式二</h1>

<img src="<?php echo $qrCode->getDataUri(); ?>" style="border:1px solid #ccc;" />

</script>
</body>
</html>